package com.sales.controller;

import com.sales.excption.SalesException;
import com.sales.service.ProfileService;
import com.sales.signUpReq.ProfileSignUpReq;
import com.sales.util.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * Created by lihu on 2017/9/7.
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/v1/sales", produces = "application/json;charset=utf-8")
public class ProfileController {

    @Autowired
    ProfileService profileService;


    /**
     * 账号信息更新
     *
     * @param profileSignUpReq
     * @param principal
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/profile")
    public ResponseEntity putProfile(@RequestBody ProfileSignUpReq profileSignUpReq,
                                     Principal principal) {
        try {
            //上传身份证号： 身份证号校验
            if (StringUtils.isNotBlank(profileSignUpReq.getIdCardNo()) &&
                    !ValidatorUtil.isIDCard(profileSignUpReq.getIdCardNo())) {
                log.info("【身份证位数有误】, IdCarNo={}", profileSignUpReq.getIdCardNo());
                return new ResponseEntity("身份证位数有误",
                        HttpStatus.BAD_REQUEST);
            }
            //上传银行卡：银行卡号校验
            if (StringUtils.isNotBlank(profileSignUpReq.getBankCardNo()) &&
                    !ValidatorUtil.matchLuhn(profileSignUpReq.getBankCardNo())) {
                log.info("【银行卡号有误】, BankCardNo={}", profileSignUpReq.getBankCardNo());
                return new ResponseEntity("银行卡号位数有误",
                        HttpStatus.BAD_REQUEST);
            }
            profileSignUpReq.setUsername(principal.getName());
            //更新
            profileService.putProfile(profileSignUpReq);
            return new ResponseEntity(HttpStatus.OK);
        } catch (SalesException ex) {
            log.error("putProfile is error=>profileSignUpReq:{}", profileSignUpReq, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("putProfile is error=>profileSignUpReq:{}", profileSignUpReq, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    /**
     * 获取用户基本信息
     *
     * @param principal
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/profile")
    public ResponseEntity getProfile(Principal principal) {
        try {
            return new ResponseEntity(profileService.getProfile(principal.getName()), HttpStatus.OK);
        } catch (SalesException ex) {
            log.error("getProfile is error=>principal:{}", principal, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("getProfile is error=>principal:{}", principal, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}
